<template>
    <div style="">
        <div class="container" v-show="!showEdit">
            <div class="handle-box">
                <!-- <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button> -->
                <el-input v-model="title" placeholder="标题" class="handle-input mr10"></el-input>
                <el-date-picker
                    v-model="timeRange"
                    type="daterange"
                    range-separator="-"
                    start-placeholder="推送时间"
                    end-placeholder="推送时间"
                    value-format="yyyy-MM-dd"
                    style="width: 230px; margin-right: 10px"
                >
                </el-date-picker>
                <el-select v-model="status" placeholder="状态" class="handle-select mr10">
                    <el-option key="-1" label="全部" value="-1"></el-option>
                    <el-option key="0" label="未推送" value="0"></el-option>
                    <el-option key="1" label="已推送" value="1"></el-option>
                </el-select>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
            </div>
            <div style=" display: flex;justify-content: flex-end;margin-bottom: 20px;">
                <el-button type="primary" @click="addAccount">新建消息</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <el-table-column type="index" :index="methodIndex" label="序号" width="55" align="center"></el-table-column>
                <el-table-column prop="title" label="标题"></el-table-column>
                <el-table-column label="创建时间" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.createTime | dateFilter }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="推送时间" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.sendTime | dateFilter }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="sendUserName" label="推送账号"></el-table-column>
                <el-table-column label="状态" width="80" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.status == 1 ? '已推送' : '未推送' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" v-if="scope.row.status == 1" @click="handleShow(scope.$index, scope.row)">查看</el-button>
                        <el-button type="text" v-if="scope.row.status == 0" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" v-if="scope.row.status == 0" @click="handlePush(scope.$index, scope.row)">推送</el-button>
                        <el-button type="text" v-if="scope.row.status == 0" @click="handleCancel(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next,jumper"
                    :current-page="pageNo"
                    :page-size="pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <edit-msg v-if="showEdit" :isAdd="isAdd" :msgId="msgId" @back="back" @save="save"></edit-msg>

        <!-- 推送弹出框 -->
        <el-dialog class="popView" :visible.sync="isPush" v-if="isPush" :show-close="false" width="200px">
            <div class="main">
                <span class="title">是否推送该消息？</span>
                <div class="btns">
                    <el-button @click="isPush = false">取 消</el-button>
                    <el-button type="primary" @click="actionPush">推送</el-button>
                </div>
            </div>
        </el-dialog>
        <!-- 推送弹出框 -->
        <el-dialog class="popView" :visible.sync="isCancel" v-if="isCancel" :show-close="false" width="200px">
            <div class="main">
                <span class="title">请确认是否删除？</span>
                <div class="btns">
                    <el-button @click="isCancel = false">取 消</el-button>
                    <el-button type="primary" @click="actionCancle">删除</el-button>
                </div>
            </div>
        </el-dialog>
        <!-- 查看弹出框 -->
        <el-dialog class="popView" :visible.sync="isShow" v-if="isShow" :show-close="false" width="375px">
            <div class="main">
                <span class="title">{{ selItem.title }}</span>
                <iframe class="web" :srcdoc="content" frameborder="0"></iframe>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
import editMsg from '../common/editMsg';
import EditMsg from '../common/editMsg.vue';
export default {
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            delList: [],

            title: null,
            sendTime: null,
            status: null,
            pageNo: 1,
            pageSize: 10,
            selItem: null,
            pageTotal: 0,
            showEdit: false,
            msgId: 0,
            isAdd: false,
            isPush: false,
            isCancel: false,
            isShow: false,
            content: '',
            timeRange:null,
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.$api
                .post({
                    url: '/systemMsg/pageQuery',
                    data: {
                        pageNo: this.pageNo,
                        pageSize: this.pageSize,
                        param: {
                            title: this.title,
                            sendTimeStart: this.timeRange ? this.timeRange[0] + ' 00:00:00' : null,
                            sendTimeEnd: this.timeRange ? this.timeRange[1] + ' 00:00:00' : null,
                            status: this.status == -1 ? null : this.status
                        }
                    }
                })
                .then(res => {
                    if (res.code && res.code == '200') {
                        this.tableData = res.data.result || [];
                        this.pageTotal = res.data.total;
                    }
                });
        },
        // 触发搜索按钮
        handleSearch() {
            this.pageNo = 1;
            this.getData();
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.showEdit = true;
            this.msgId = row.id;
            this.isAdd = false;
            // this.handleShow(index, row);
        },
        // 查看操作
        handleShow(index, row) {
            this.isShow = true;
            this.selItem = row;
            let self = this;
            self.$api
                .get({
                    url: '/systemMsg/findById',
                    data: {
                        id: row.id
                    }
                })
                .then(res => {
                    console.log('🚀 ~ file: editBanner.vue ~ line 156 ~ getData ~ res', res);
                    self.content = res.data.content || '';
                })
                .catch(err => {});
        },
        // 推送操作
        handlePush(index, row) {
            this.isPush = true;
            this.selItem = row;
        },
        actionPush() {
            this.$api
                .get({
                    url: '/systemMsg/send',
                    data: {
                        id: this.selItem.id
                    }
                })
                .then(res => {
                    this.isPush = false;
                    this.$message.success('推送成功');
                    this.getData();
                })
                .catch(err => {});
        },
        // 删除操作
        handleCancel(index, row) {
            this.isCancel = true;
            this.selItem = row;
        },
        actionCancle() {
            this.$api
                .get({
                    url: '/systemMsg/delById',
                    data: {
                        id: this.selItem.id
                    }
                })
                .then(res => {
                    this.isCancel = false;
                    this.$message.success('删除成功');
                    this.getData();
                });
        },
        // 新增
        addAccount() {
            this.showEdit = true;
            this.isAdd = true;
            this.msgId = 0;
        },
        back() {
            this.selItem = null;
            this.showEdit = false;
        },
        // 保存
        save() {
            this.selItem = null;
            this.showEdit = false;
            this.getData();
        },
        // 分页导航
        handlePageChange(val) {
            this.pageNo = val;
            this.getData();
        },
        methodIndex(index) {
            return index + 1;
        }
    },
    components: {
        editMsg
    }
};
</script>

<style scoped lang="scss">
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 100px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
/deep/.popView {
    display: flex;
    align-items: center;
    justify-content: center;
    .el-dialog__header {
        padding: 0;
    }
    .el-dialog__body {
        padding: 0;
    }
    .main {
        display: flex;
        flex-direction: column;
        align-items: center;
        background-color: #fff;
        border-radius: 5px;
        padding: 20px 0;
        .title {
            font-size: 17px;
            font-weight: bold;
            margin-bottom: 20px;
        }
        .btns {
            width: 100%;
            display: flex;
            justify-content: space-around;
        }
        .web {
            width: 375px;
        }
    }
}
</style>
